/**
 * 字牌JAVA回放
 * Created by duwei on 2019/4/16.
 */
var Zipai_PlayerBackJavaWindow = PlayBackWindow.extend({
    curRound: 0,
    zongRound: 0,
    PLAY_INTERVAL: 0.8,
    /**
     * 初始化工程资源
     * @private
     */
    _ccsFileName: res.Zipai_PlayerBackWindow_json,
    /**
     * 点击事件
     * @private
     */
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'root.backward': '_clickBackward',
            'root.back': '_clickBack',
            'root.toward': '_clickToward',
            'root.btn_pause': '_clickPause',
            'root.btn_play': '_clickPlay',
            'root.nextRound': '_clickNextRound',
            'root.beforeRound': '_clickBeforeRound'
        };
    },
    /**
     * 快退
     * @private
     */
    _clickBackward: function () {
        var self = this;
        self.unscheduleAllCallbacks();
        for (self.curDataIndex = Math.max(self.curDataIndex - 1, 0); self.curDataIndex >= 0; self.curDataIndex--) {
            if (self.data[self.curDataIndex] && !self.data[self.curDataIndex].code) {
                var uid4001 = 0;
                for (var i = self.curDataIndex + 1; i < self.data.length && self.data[i].code; i++)
                    if (self.data[i].code == 4001 || self.data[i].code == 4003) {
                        uid4001 = self.data[i]['data'].uid;
                        break;
                    }
                if (uid4001)
                    self.room.throwTurnByUid(uid4001);
                cc.director.resume();
                self.room.setReplayProgress(self.curDataIndex, self.data.length - 1);
                self.room.setAllPai4Replay(self.data[self.curDataIndex]);
                self.scheduleOnce(function () {
                    self.popCommand();
                }, self.PLAY_INTERVAL / 2);
                break;
            }
        }
        if (self.curDataIndex < 0) {
            self.curDataIndex = 0;
            self.room.setReplayProgress(0, 1);
            self.scheduleOnce(function () {
                self.popCommand();
            }, self.PLAY_INTERVAL / 2);
        }
    },
    /**
     * 点击返回
     * @private
     */
    _clickBack: function () {
        var self = this;
        cc.director.resume();
        showMessageBox('是否要退出当前回放?', function () {
            showBoard('HallBoard');
        });
    },
    /**
     * 点击快进
     * @private
     */
    _clickToward: function () {
        var self = this;
        self.unscheduleAllCallbacks();
        for (self.curDataIndex = Math.min(self.curDataIndex + 1, (self.data.length > 0) ? self.data.length - 1 : 0); self.curDataIndex < self.data.length; self.curDataIndex++) {
            if (self.data[self.curDataIndex] && !self.data[self.curDataIndex].code) {
                var uid4001 = 0;
                for (var i = self.curDataIndex + 1; i < self.data.length && self.data[i].code; i++)
                    if (self.data[i].code == 4001 || self.data[i].code == 4003) {
                        uid4001 = self.data[i]['data'].uid;
                        break;
                    }
                if (uid4001)
                    self.room.throwTurnByUid(uid4001);
                cc.director.resume();
                self.room.setReplayProgress(self.curDataIndex, self.data.length - 1);
                self.room.setAllPai4Replay(self.data[self.curDataIndex]);
                self.scheduleOnce(function () {
                    self.popCommand();
                }, self.PLAY_INTERVAL / 2);
                break;
            }
        }

        if (self.curDataIndex >= self.data.length) {
            self.curDataIndex = self.data.length - 1;
            self.room.setReplayProgress(1, 1);
        }
    },
    /**
     * 点击暂停
     * @private
     */
    _clickPause: function () {
        this.btn_play.setVisible(true);
        this.btn_pause.setVisible(false);
        cc.director.pause();
        this.status = !this.status;
    },
    /**
     * 点击开始
     * @private
     */
    _clickPlay: function () {
        this.btn_play.setVisible(false);
        this.btn_pause.setVisible(true);
        cc.director.resume();
        this.status = !this.status;
    },
    /**
     * 点击下一局
     * @private
     */
    _clickNextRound: function () {
        if (this.curRound >= this.zongRound) {
            return;
        }
        this.nextRoundPlay(true);
    },
    /**
     * 点击上一局
     * @private
     */
    _clickBeforeRound: function () {
        if (this.curRound <= 1) {
            return;
        }
        this.nextRoundPlay(false);
    },
    /**
     * 初始化界面
     * @param data
     * @private
     */
    _initPlayBackWindow: function (data) {
        var self = this;
        self.replayData = data;
        this.btn_backward = self._nodeList['root.backward'];
        this.btn_backward2 = self._nodeList['root.backward2'];
        this.btn_back = self._nodeList['root.back'];
        this.btn_toward = self._nodeList['root.toward'];
        this.btn_toward2 = self._nodeList['root.toward2'];
        this.btn_pause = self._nodeList['root.btn_pause'];
        this.btn_play = self._nodeList['root.btn_play'];
        this.nextRound = self._nodeList['root.nextRound'];
        this.beforeRound = self._nodeList['root.beforeRound'];

        this.btn_backward2.setVisible(false);
        this.btn_toward2.setVisible(false);
        this.curRound = parseInt(gameData.recordIds[1]);
        this.zongRound = gameData.totalRound;

        var jsondata = data || {};
        self.resp4008 = _.isArray(jsondata["4008"]) ? jsondata["4008"][0] : null;
        network.selfRecv(jsondata["3002"] || {});

        var idArrS = jsondata["3005"] || [];
        for (var i = 0; i < idArrS.length; i++) {
            if (idArrS[i]["data"]["uid"] == gameData.uid) {
                network.selfRecv(idArrS[i]);
                break;
            }
        }
        if (i == idArrS.length && idArrS.length != 0) {
            network.selfRecv(idArrS[0]);
        }
        self.data = deepCopy(jsondata["arr"]) || [];
        //目前是暂停还是播放状态 true是播放 false暂停
        this.status = true;
        self.room.setAfterGameStart(function () {
            //第一条数据
            for(self.curDataIndex ; self.curDataIndex<self.data.length;) {
                var date = deepCopy(self.data[self.curDataIndex]);
                if(date['code']){
                    network.selfRecv(date);
                    self.curDataIndex++
                }else{
                    self.room.setAllPai4Replay(date);
                    self.scheduleId = self.scheduleOnce(function () {
                        self.popCommand();
                    }, 0.01);
                    break;
                }
            }
        });
    },
    /**
     * 下一个消息
     */
    popCommand: function () {
        var self = this;
        for (self.curDataIndex++; self.curDataIndex < self.data.length; self.curDataIndex++) {
            if (self.data[self.curDataIndex].code) {
                cc.log('popCommand == '+self.data[self.curDataIndex].code);
                self.room.setReplayProgress(self.curDataIndex, self.data.length - 1);
                if (self.curDataIndex == self.data.length - 1 && self.resp4008) {
                    network.selfRecv(self.resp4008);
                }
                network.selfRecv(self.data[self.curDataIndex]);
                self.scheduleOnce(function () {
                    self.popCommand();
                }, self.PLAY_INTERVAL);
                break;
            }
        }
    },
    checkBtnVisible: function () {
        if (this.curRound <= 1) {
            this.beforeRound.setVisible(false);
        } else {
            this.beforeRound.setVisible(true);
        }
        if (this.curRound >= this.zongRound) {
            this.nextRound.setVisible(false);
        } else {
            this.nextRound.setVisible(true);
        }
    },
    /**
     * 获取其他局回放
     * @param isNext
     */
    nextRoundPlay: function (isNext) {
        var self = this;
        cc.director.resume();
        var recordId = gameData.recordIds[0] + '-' + (!isNext ? (parseInt(gameData.recordIds[1]) - 1) : (parseInt(gameData.recordIds[1]) + 1));
        var str = (isNext ?'下一局':'上一局')+'回放';
        showLoading('正在获取'+str + '...');
        var replayurl = app.recordPath[1] + '/' + app.recordPath[2] + recordId;
        console.log('replayurl = ' + replayurl);
        gameData.recordIds = recordId.split('-');
        http.get(replayurl, function (data) {
            var tData = data.replace(/'/g, "\"");
            data = JSON.parse(tData);
            if (data['3002']) {
                gameData.mapId = data['3002']['data']['map_id'];
            } else {
                var option = JSON.parse(data[0]['data']['Option']);
                gameData.mapId = option['mapid'];
            }
            SubUpdateUtil.checkUpdateSubByMapId(gameData.mapId,function (sub) {
                showRoom(data, ROOM_DATA_TYPE.REPLAY);
            });
        }, function () {
            hideLoading();
            showMessageBox('未获取'+str + '数据,可能房间已解散。');
            self.zongRound = self.curRound;
            self.checkBtnVisible();
        });
    },
});